package com.jzy.message.util.json;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;
/**
 * Created by Administrator on 2016/6/13.
 */
public class JsonUtil {
    /**
     * 将json转化为实体POJO
     * @param jsonStr
     * @param obj
     * @return
     */
    public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
        T t = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            t = objectMapper.readValue(jsonStr,
                    obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
    //jsonStr:要解析的json字符串
    //obj 要解析成的类包括数组型的类
    //DataNodeName 要解析生成数组的数据节点名称
    public static<T> Object JSONToObj(String jsonStr,Class<T> obj,String DataNodeName) {
        T t = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JSONObject jsonObject = new JSONObject(jsonStr);// new JSONObject(resultcontentString);
            String jsonStrData = jsonObject.getString(DataNodeName);
            t = objectMapper.readValue(jsonStrData,obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
    //jsonStr:要解析的json字符串
    //obj 要解析成的类包括数组型的类
    //DataNodeName 要解析生成数组的数据节点名称
    public static<T> Object JSONToObj(String jsonStr,Class<T> obj,String DataNodeName,String DataNodeName2) {
        T t = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JSONObject jsonObject = new JSONObject(jsonStr);// new JSONObject(resultcontentString);
            String jsonStrData = jsonObject.getString(DataNodeName);

            JSONObject jsonObject2=new JSONObject(jsonStrData);
            String jsonStrData2=jsonObject2.getString(DataNodeName2);
            t = objectMapper.readValue(jsonStrData2,obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
    /**
     * 将实体POJO转化为JSON
     * @param obj
     * @return
     * @throws JSONException
     * @throws IOException
     */
    public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        // Convert object to JSON string
        String jsonStr = "";
        try {
            jsonStr =  mapper.writeValueAsString(obj);
        } catch (IOException e) {
            throw e;
        }
        return new JSONObject(jsonStr);
    }
    public static String getCodeOfResponse(String jsonstr)
    {
        if(jsonstr.equals(""))
            return "";
        try
        {
            JSONObject jsonObject = new JSONObject(jsonstr);
            String Code = jsonObject.getString("code");

            return Code;
        }
        catch (Exception e)
        {
            return "";
        }

    }
    public static String getCodeOfResponse1(String jsonstr)
    {
        if(jsonstr.equals(""))
            return "";
        try
        {
            JSONObject jsonObject = new JSONObject(jsonstr);
            String Code = jsonObject.getString("code");
            return Code;
        }
        catch (Exception e)
        {
            return "";
        }
    }
}
